package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ParameterErrorException;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.ProductService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2023年06月07日 9:47
 */
public class MainController {

    // 当前登录的用户
    private User user;
    // 当前成功登录的用户的购物车
    private Cart cart = new Cart();
    private Scanner sc = new Scanner(System.in);

    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");

    /**
     * 由于该页面用户必须是在成功登录之后才能访问
     * 因此,在其构造方法中要求必须传递登录后的用户信息
     *
     * @param user
     */
    public MainController(User user) {
        this.user = user;
    }

    public void showMenu() {
        System.out.println("***************欢迎登录网络商城管理系统***************");
        System.out.print("请选择服务(1.加入购物车 2.查看购物车 3.我的订单 4.修改密码 5.注销):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            default:
                System.out.println("选项有误!");
                break;
        }
    }

    public void addCart(){
        List<Product> products = productService.findAll();
        if(!products.isEmpty()){
            System.out.println("------------------------------------");
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for(Product product : products){
                System.out.println(product.getId()+"\t\t"+product.getName()+"\t\t"+product.getPrice());
            }
            System.out.println("------------------------------------");
        }
        System.out.print("请选择需要加入到购物车的商品编号:");
        int id = sc.nextInt();

        try {
            cartService.addCart(cart,id);
            System.out.println("添加成功");
        } catch (UserNotLoginException e) {
            System.out.println("添加失败,失败原因:"+e.getMessage());
        } catch (ParameterErrorException e) {
            System.out.println("添加失败,失败原因:"+e.getMessage());
        } catch (ProductNotExistException e) {
            System.out.println("添加失败,失败原因:"+e.getMessage());
        }
        showMenu();
    }

    public void showCart(){
        System.out.println("编号\t\t商品名称\t\t数量\t\t总价");
        for(Item item : cart.getItems()){
            System.out.println(item.getProduct().getId()+"\t\t"+item.getProduct().getName()+"\t\t"+item.getNum()+"\t\t"+item.getPrice());
        }
        System.out.println("--------------------------------------------");
        System.out.println("购物车总价:"+cart.getPrice());
        System.out.println("--------------------------------------------");

        System.out.print("请选择(1.删除商品 2.修改商品数量 3.清空购物车 4.结算 5.返回主菜单):");
        int choice = sc.nextInt();
        switch (choice){
            case 5:
                showMenu();
        }
    }
}
